package com.ziq.baselib.business.post

import android.annotation.SuppressLint
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.facebook.imagepipeline.request.ImageRequest
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.base.utils.ImageUtil
import com.ziq.baselib.business.post.bean.PostingDetail
import com.ziq.baselib.databinding.ItemPostingBinding

class PostingAdapter(val itemWidth:Int) : RecyclerView.Adapter<BindingViewHolder<ItemPostingBinding>>(){

    private var dataList = mutableListOf<PostingDetail>()
    private var postingAdapterAction: PostingAdapterAction? = null

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: MutableList<PostingDetail>) {
        dataList = list
        notifyDataSetChanged()
    }

    fun setPostingAdapterAction(postingAdapterAction: PostingAdapterAction){
        this.postingAdapterAction = postingAdapterAction
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemPostingBinding> {
        return createBindingViewHolder(parent)
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: BindingViewHolder<ItemPostingBinding>, position: Int) {
        val item = dataList[position]
        holder.itemView.click {
            postingAdapterAction?.itemClick(item, holder)
        }

        var content = "无内容"
        if(item.post?.title?.isNotEmpty() == true){
            content = "${item.post?.title}"
        }else if(item.post?.content?.isNotEmpty() == true){
            content = "${item.post?.content}"
        }
        holder.binding.title.text =  "$content $position"
        val imageInfo = item.post?.images?.get(0)
        if(imageInfo != null){
            val params = holder.binding.image.layoutParams as ViewGroup.LayoutParams
            params.width = itemWidth
            params.height = calculatePictureViewHeight(itemWidth, imageInfo.width, imageInfo.height)
            holder.binding.image.layoutParams = params

            ImageUtil.updateImageFromNetwork(holder.binding.image, imageInfo.url, ImageRequest.CacheChoice.DEFAULT)

        } else {
            val params = holder.binding.image.layoutParams as ViewGroup.LayoutParams
            params.width = itemWidth
            params.height = itemWidth
            holder.binding.image.layoutParams = params
        }

        holder.binding.title.transitionName = "title"
        holder.binding.image.transitionName = "image"

    }

    private fun calculatePictureViewHeight(itemWidth:Int, pictureWidth:Int?, pictureHeight:Int?): Int{
        val result = if(pictureWidth != null && pictureWidth > 0
            && pictureHeight != null && pictureHeight > 0){
            val ratio = pictureWidth.toFloat() / pictureHeight.toFloat()
            if(ratio > 5f / 3f){
                (itemWidth.toFloat() * 3f / 5f).toInt()
            }else if(ratio < 5f / 6f){
                (itemWidth.toFloat() * 6f / 5f).toInt()
            }else {
                (itemWidth.toFloat() * pictureHeight.toFloat() / pictureWidth.toFloat()).toInt()
            }
        } else {
            (itemWidth.toFloat() * 6f / 5f).toInt()
        }
        return result
    }


}


interface PostingAdapterAction{
    fun itemClick(item: PostingDetail, holder: BindingViewHolder<ItemPostingBinding>)
}